﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MisClient
{
    public partial class FrmDayJs : Form
    {
        private string opState;

        private IBLL.IDayJS typeBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteDayJS();

        public FrmDayJs()
        {
            InitializeComponent();
        }

        private void btnSek_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            btn.FlatAppearance.BorderSize = 0;
            Point point = new Point(btn.Left + this.Left + 5, btn.Top + this.Top + 40);
            contextMenuAssign.Show(point);
        }

        private void ByCodeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string value = InputBox.Input("指定.按编码", "请输入编码：", "");
            if (!string.IsNullOrEmpty(value))
            {
               MisModel.DayJs model= typeBLL.GetById(value.Trim());
               if (model != null)
               {
                   this.txtKhjsZqCode.Text = model.DayJS.ToString();
                   this.txtKhjsZqMark.Text = model.F1;
               }
            }
        }

        private void ChoiseListToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.lbxList.DisplayMember = "ItemShow";
            this.lbxList.DataSource = typeBLL.GetAll();
            this.lbxList.Show();
        }

        private void lbxList_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            DataRowView view = this.lbxList.SelectedItem as DataRowView;
            this.txtKhjsZqCode.Text = view.Row["DAY_JS"].ToString();
            this.txtKhjsZqMark.Text = view.Row["F1"].ToString();
            this.lbxList.Hide();

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.txtKhjsZqCode.ReadOnly = false;
            this.txtKhjsZqMark.ReadOnly = false;
            this.btnAdd.Enabled = false;
            this.btnDel.Enabled = false;
            this.btnMod.Enabled = false;
            this.btnSek.Enabled = false;
            this.btnPrint.Enabled = false;
            this.btnRef.Enabled = false;
            this.btnQuit.Enabled = false;
            this.btnOk.Enabled = true;
            this.btnCan.Enabled = true;

            this.txtKhjsZqCode.Text = "";
            this.txtKhjsZqMark.Text = "";
            this.txtKhjsZqCode.Focus();
            this.opState = "添加";
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnCan_Click(object sender, EventArgs e)
        {
            this.txtKhjsZqCode.ReadOnly = true;
            this.txtKhjsZqMark.ReadOnly = true;
            this.btnAdd.Enabled = true;
            this.btnDel.Enabled = true;
            this.btnMod.Enabled = true;
            this.btnSek.Enabled = true;
            this.btnPrint.Enabled = true;
            this.btnRef.Enabled = true;
            this.btnQuit.Enabled = true;
            this.btnOk.Enabled = false;
            this.btnCan.Enabled = false;
            this.btnAdd.Focus();

            DataRowView view = this.lbxList.SelectedItem as DataRowView;
            if (view != null)
            {
                this.txtKhjsZqCode.Text = view.Row["DAY_JS"].ToString();
                this.txtKhjsZqMark.Text = view.Row["F1"].ToString();
            }
        }

        private void btnMod_Click(object sender, EventArgs e)
        {
            this.txtKhjsZqCode.ReadOnly = true;
            this.txtKhjsZqMark.ReadOnly = false;
            this.btnAdd.Enabled = false;
            this.btnDel.Enabled = false;
            this.btnMod.Enabled = false;
            this.btnSek.Enabled = false;
            this.btnPrint.Enabled = false;
            this.btnRef.Enabled = false;
            this.btnQuit.Enabled = false;
            this.btnOk.Enabled = true;
            this.btnCan.Enabled = true;
            this.opState = "修改";
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.opState))
            {
                bool isOk = false;
                if (this.opState == "添加")
                {
                   isOk =  Add();
                }
                else if (this.opState == "修改")
                {
                   isOk= Mod();
                }

                if (isOk)
                {
                    this.txtKhjsZqCode.ReadOnly = true;
                    this.txtKhjsZqMark.ReadOnly = true;
                    this.btnAdd.Enabled = true;
                    this.btnDel.Enabled = true;
                    this.btnMod.Enabled = true;
                    this.btnSek.Enabled = true;
                    this.btnPrint.Enabled = true;
                    this.btnRef.Enabled = true;
                    this.btnQuit.Enabled = true;
                    this.btnOk.Enabled = false;
                    this.btnCan.Enabled = false;
                    this.btnAdd.Focus();
                }
            }
        }

        //添加支付工具编码
        protected bool Add()
        {
            if (this.txtKhjsZqCode.Text.Trim().Length == 0)
            {
                MessageBox.Show("客户结算周期不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtKhjsZqCode.Focus();
                return false;
            }

            MisModel.DayJs obj = new MisModel.DayJs();
            obj.DayJS =int.Parse(this.txtKhjsZqCode.Text.Trim());
            obj.F1 = this.txtKhjsZqMark.Text.Trim();
            int ok = typeBLL.Add(obj);
            if (ok == -1)
            {
                MessageBox.Show("操作失败！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }
            else
            {
                MessageBox.Show("本条记录已成功添加！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            return true;
        }

        //保存支付工具编码
        protected bool Mod()
        {
            MisModel.DayJs obj = new MisModel.DayJs();
            obj.DayJS = int.Parse(this.txtKhjsZqCode.Text.Trim());
            obj.F1 = this.txtKhjsZqMark.Text.Trim();
            int ok = typeBLL.Update(obj);
            if (ok == -1)
            {
                MessageBox.Show("操作失败！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }
            else
            {
                MessageBox.Show("本条记录已成功修改！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            return true;
        }

        private void txtStatName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtKhjsZqMark.Focus();
            }
        }

        private void txtMark_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btnOk.Focus();
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            string statCode = this.txtKhjsZqCode.Text.Trim();
            if (string.IsNullOrEmpty(statCode))
                return;

            if (MessageBox.Show("请确认删除[y/n]？", "提问窗", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                int ok = typeBLL.Delete(statCode);
                if (ok == -1)
                {
                    MessageBox.Show("操作失败！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    MessageBox.Show("本条记录已成功删除！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.txtKhjsZqCode.Text = "";
                    this.txtKhjsZqMark.Text = "";
                    this.btnDel.Enabled = false;
                    this.btnMod.Enabled = false;
                }
            }
        }
    }
}
